package com.iclub.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iclub.dao.IClubUserDao;
import com.iclub.model.ApplyFriend;
import com.iclub.model.IClubUser;
import com.iclub.service.IClubUserService;
import com.iclub.util.Constants;

@Service
public class IClubUserServiceImpl extends BaseService implements IClubUserService{
	

	@Override
	public Map<String,Object> selectIClubUser(Map<String, String> map) {
		Map<String,Object> res=new HashMap<String, Object>();
		IClubUser user=null;
		try {
			user=iClubUserDao.selectIClubUser(map);
			user.setPassword("");
			if(user!=null&&user.getUserId()!=null){
				res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
				res.put(Constants.KEY_RESCONTENT, user);
			}
			else{
				res.put(Constants.KEY_RESCODE, Constants.NORESULT_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.NORESULT_RESMSG);
			}
		} catch (SQLException e) {
			log_detail.error("查询用户信息,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String,Object> insertIClubUser(IClubUser user) {
		Map<String,Object> res=new HashMap<String, Object>();
		try {
			iClubUserDao.insertIClubUser(user);
			res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
			res.put("userId", user.getUserId());
		} catch (SQLException e) {
			log_detail.error("用户注册,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}
	@Override
	public Map<String,Object> updateUser(IClubUser user) {
		Map<String,Object> res=new HashMap<String, Object>();
		try {
			iClubUserDao.updateUser(user);
			res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
		} catch (SQLException e) {
			log_detail.error("用户更新信息,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, Object> selectIClubUserList(Map<String, String> map) {
		Map<String,Object> res=new HashMap<String, Object>();
		List<IClubUser> userlist=null;
		try {
			userlist=iClubUserDao.selectFriendList(map);
			if(userlist!=null&&userlist.size()>0){
				res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
				res.put(Constants.KEY_RESCONTENT, userlist);
			}
			else{
				res.put(Constants.KEY_RESCODE, Constants.NORESULT_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.NORESULT_RESMSG);
			}
		} catch (SQLException e) {
			log_detail.error("查询好友列表,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, Object> addFriend(Map<String, String> map) {
		Map<String,Object> res=new HashMap<String, Object>();
		try {
			iClubUserDao.addFriend(map);
			res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
		} catch (SQLException e) {
			log_detail.error("添加好友,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, Object> delFriend(Map<String, String> map) {
		Map<String,Object> res=new HashMap<String, Object>();
		try {
			iClubUserDao.delFriend(map);
			res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
		} catch (SQLException e) {
			log_detail.error("删除好友,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, String> checkRegist(Map<String,String> map) {

		Map<String,String> res=new HashMap<String, String>();
		IClubUser user=null;
		try {
			user=iClubUserDao.selectIClubUser(map);
			if(user!=null&&user.getUsername()!=null){
				res.put(Constants.KEY_RESCODE, Constants.NORESULT_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.ALREADYREGIST_RESMSG);
			}
			else{
				res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
				
			}
		} catch (SQLException e) {
			log_detail.error("校验用户是否注册,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	
	}

	@Override
	public Map<String, Object> checkIClubUser(Map<String, String> map) {
		Map<String,Object> res=new HashMap<String, Object>();
		IClubUser user=null;
		try {
			user=iClubUserDao.checkIClubUser(map);
			user.setPassword("");
			if(user!=null&&user.getUserId()!=null){
				res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
				res.put(Constants.KEY_RESCONTENT, user);
			}
			else{
				res.put(Constants.KEY_RESCODE, Constants.NORESULT_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.NORESULT_RESMSG);
			}
		} catch (SQLException e) {
			log_detail.error("用户登录,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, Object> searchUserList(Map<String, String> map)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> applyFriend(ApplyFriend apply)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> selectApplyFriendMsgList(Map<String, String> map)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
